package org.duang.http.util;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.duang.http.base.HttpRes;
import org.duang.kit.HttpKit;
import org.duang.kit.HttpKit.ContentTypeEnum;  
public class HttpsClient {

	  
	  
	    public static void main(String args[]) {  
	  
//	        try {  
//	  
//	            HttpClient httpclient = new DefaultHttpClient();  
//	                        //Secure Protocol implementation.  
//	            SSLContext ctx = SSLContext.getInstance("SSL");  
//	                        //Implementation of a trust manager for X509 certificates  
//	            X509TrustManager tm = new X509TrustManager() {  
//	  
//	                public void checkClientTrusted(X509Certificate[] xcs,  
//	                        String string) throws CertificateException {  
//	  
//	                }  
//	  
//	                public void checkServerTrusted(X509Certificate[] xcs,  
//	                        String string) throws CertificateException {  
//	                }  
//	  
//	                public X509Certificate[] getAcceptedIssuers() {  
//	                    return null;  
//	                }  
//	            };  
//	            ctx.init(null, new TrustManager[] { tm }, null);  
//	            SSLSocketFactory ssf = new SSLSocketFactory(ctx);  
//	  
//	            ClientConnectionManager ccm = httpclient.getConnectionManager();  
//	                        //register https protocol in httpclient's scheme registry  
//	            SchemeRegistry sr = ccm.getSchemeRegistry();  
//	            sr.register(new Scheme("https", 443, ssf));  
//	  
//	            HttpGet httpget = new HttpGet();
//	            HttpParams params = httpclient.getParams();  
////	            String url = "https://devapi.lovepurejoy.com/qingplus/sku?city_code=021&sign=fd2167d6f0625e50ca967d807d870703&sign_type=MD5";
//	            params.setParameter("city_code", "021");  
//	            params.setParameter("sign", "fd2167d6f0625e50ca967d807d870703");
//	            params.setParameter("sign_type", "MD5");
//	  
//	            httpget.setParams(params);
//	            URI uri = new URI("https://devapi.lovepurejoy.com/qingplus/sku");
//	            httpget.setURI(uri);
//	            System.out.println("REQUEST:" + httpget.getURI());  
//	            ResponseHandler responseHandler = new BasicResponseHandler();  
//	            String responseBody;  
//	  
//	            responseBody = httpclient.execute(httpget, responseHandler); 
//	  
//	            System.out.println(responseBody);  
//	  
//	  
//	        } catch (NoSuchAlgorithmException e) {  
//	            e.printStackTrace();  
//	        } catch (ClientProtocolException e) {  
//	            e.printStackTrace();  
//	        } catch (IOException e) {  
//	            e.printStackTrace();  
//	        } catch (Exception ex) {  
//	            ex.printStackTrace();  
//	  
//	        }  
	    	
	    	String url = "http://devapi.lovepurejoy.com/qingplus/sku?city_code=021&sign=fd2167d6f0625e50ca967d807d870703&sign_type=MD5";
////	    	String url = "http://42.96.139.238:9200/syt/FitNutrientV5/_count";
			HttpRes res = HttpKit.duang().url(url).type(ContentTypeEnum.TEXT_HTML).get();
			System.out.println(res.getResult());
//	    	CloseableHttpClient httpClient = createSSLClientDefault();
//	    	HttpGet get = new HttpGet();
//	    	try {
//				get.setURI(new URI("http://devapi.lovepurejoy.com/qingplus/sku?city_code=021&sign=fd2167d6f0625e50ca967d807d870703&sign_type=MD5"));
//				ResponseHandler responseHandler = new BasicResponseHandler();  
//				String responseBody = httpClient.execute(get, responseHandler);				
//				System.out.println(responseBody);
////				CloseableHttpResponse response = httpClient.execute(get);
////				System.out.println(ToolsKit.toJsonString(response));
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
	    	
	    }  
	    
	    
	    public static CloseableHttpClient createSSLClientDefault(){
	    	try {
	    	             SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
	    	                 //信任所有
	    	                 public boolean isTrusted(X509Certificate[] chain,
	    	                                 String authType) throws CertificateException {
	    	                     return true;
	    	                 }
	    	             }).build();
	    	             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
	    	             return HttpClients.custom().setSSLSocketFactory(sslsf).build();
	    	         } catch (KeyManagementException e) {
	    	             e.printStackTrace();
	    	         } catch (NoSuchAlgorithmException e) {
	    	             e.printStackTrace();
	    	         } catch (KeyStoreException e) {
	    	             e.printStackTrace();
	    	         }
	    	         return  HttpClients.createDefault();
	    	}
}
